package com.chookss.video

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chookss.mine.entity.MineInfo
import com.chookss.mine.entity.MineInfoResponse
import com.chookss.tiku.entity.LiveResult
import com.chookss.tools.httpForm
import com.johnrambo.ktea.base.BaseViewModel
import com.johnrambo.ktea.common.toJsonObject
import com.johnrambo.ktea.net.http.Method
import com.johnrambo.ktea.net.http.http
import org.json.JSONObject

/**
 * Create by JohnRambo at 2018/12/17
 */
class MineModel : BaseViewModel() {
    val errorLiveData by lazy { MutableLiveData<String>() }
    val mineLiveData by lazy { MutableLiveData<JSONObject>() }

    fun getMineHomeData(
        employeeCode: String,
        timestamp: String,
        nonce: String,
        sign: String
    ) {
        jobs + http{
            url = "api/sys/my/homePage/getMyInfo"
            method = Method.POST
            params = mutableMapOf(
                "employeeCode" to employeeCode,
                "timestamp" to timestamp,
                "nonce" to nonce,
                "sign" to sign
            )
            onSuccess = { mineLiveData.value = it.toJsonObject() }
            onError = { errorLiveData.value = it }
        }
    }

    fun getMineHomeData2(employeeCode: String): LiveData<LiveResult<MineInfo>> {
        var liveResult = MutableLiveData<LiveResult<MineInfo>>()
        jobs + httpForm< MineInfoResponse> {
            url = "api/sys/my/homePage/getMyInfo"
            params = mutableMapOf("employeeCode" to employeeCode)
            onSuccess = {
                liveResult.postValue(LiveResult(it.data))
            }
            onError = {
                liveResult.postValue(LiveResult(Error(it)))
            }
        }
        return liveResult
    }
}